package com.fuershiye.woman.monitor.dao;

import java.math.BigInteger;
import java.util.List;

import com.fuershiye.woman.monitor.beans.IdName;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fuershiye.woman.monitor.beans.IdNamePid;
import com.fuershiye.woman.monitor.beans.Page;
import com.fuershiye.woman.monitor.model.Area;
import com.fuershiye.woman.monitor.utils.DaoSqlUtil;
import com.google.common.collect.Lists;

@Repository("areaDao")
@SuppressWarnings("unchecked")
public class AreaDao {

	@Autowired
	private HibernateDao hibernateDao;

	public void save(Area it) {
		hibernateDao.save(it);
	}

	public Area get(Long id) {
		return hibernateDao.getById(Area.class, id);
	}

	@SuppressWarnings("unchecked")
	public Area getTop() {

		String hql = "from Area ORDER BY id DESC";
		Query query = hibernateDao.getQuery(hql);
		query.setFirstResult(0);
		query.setMaxResults(1);
		List<Area> result = query.list();
		if (result == null || result.size() < 1) {
			return null;
		}
		return result.get(0);
	}
	
	@SuppressWarnings("unchecked")
	public Area findByCode(String code) {
		
		String hql = "from Area where status = 1 and code = ?";
		Query query = hibernateDao.getQuery(hql);
		query.setString(0, code);
		query.setFirstResult(0);
		query.setMaxResults(1);
		List<Area> result = query.list();
		if (result == null || result.size() < 1) {
			return null;
		}
		return result.get(0);
	}

	@SuppressWarnings("unchecked")
	public List<Area> list(String name, Long pid, Integer level, Integer status, Page page) {

    	StringBuffer sb = new StringBuffer("from Area where 1=1 ");

        sb = DaoSqlUtil.appendSql(sb, "pid", pid);
        sb = DaoSqlUtil.appendSql(sb, "level", level);
        sb = DaoSqlUtil.appendSql(sb, "status", status);
        if(StringUtils.isNotBlank(name)){
        	sb.append("and (name like:name or code like:name) ");
    	}

		sb.append("order by sort asc");
        Query query = hibernateDao.getQuery(sb.toString());

        query = DaoSqlUtil.appendQuery(query, "pid", pid);
        query = DaoSqlUtil.appendQuery(query, "level", level);
        query = DaoSqlUtil.appendQuery(query, "status", status);
        if(StringUtils.isNotBlank(name)){
            query.setString("name","%" + name + "%");
    	}
    	if(page!=null){
			query.setFirstResult(page.getStart());
			query.setMaxResults(page.getPageSize());
		}
    	
        return query.list();
	}
	
    public int getTotal(String name, Long pid, Integer level, Integer status) {
    	
    	StringBuffer sb = new StringBuffer("select count(*) from area where 1=1 ");

        sb = DaoSqlUtil.appendSql(sb, "pid", pid);
        sb = DaoSqlUtil.appendSql(sb, "level", level);
        sb = DaoSqlUtil.appendSql(sb, "status", status);
    	if(StringUtils.isNotBlank(name)){
    		sb.append("and (name like:name or code like:name) ");
    	}
    	
    	Query query = hibernateDao.getSqlQuery(sb.toString());

        query = DaoSqlUtil.appendQuery(query, "pid", pid);
        query = DaoSqlUtil.appendQuery(query, "level", level);
        query = DaoSqlUtil.appendQuery(query, "status", status);
    	if(StringUtils.isNotBlank(name)){
    		query.setString("name","%" + name + "%");
    	}

        BigInteger count = (BigInteger) query.uniqueResult();
        return (int) count.longValue();
    }

	@SuppressWarnings("unchecked")
	public Area findBySort(Long pos, Integer aim, Integer count) {

		StringBuffer sb = new StringBuffer("from Area where sort ");
		if (aim == 1) {
			sb.append(">:sort ");
		} else {
			sb.append("<:sort ");
		}
		sb.append("order by sort asc");

		Query query = hibernateDao.getQuery(sb.toString());
		query.setParameter("sort", pos);
		query.setFirstResult(0);
		query.setMaxResults(count);

		List<Area> result = query.list();
		if (result == null || result.size() < 1) {
			return null;
		}
		return result.get(0);
	}

	@SuppressWarnings("unchecked")
	public List<IdNamePid> getByLevelAndPid (Integer level, Long pid) {

		List<IdNamePid> result = Lists.newArrayList();

    	StringBuffer sb = new StringBuffer("select id, name from area where 1=1 ");
        sb = DaoSqlUtil.appendSql(sb, "pid", pid);
        sb = DaoSqlUtil.appendSql(sb, "level", level);
		sb.append("order by sort desc");
		
		Query query = hibernateDao.getSqlQuery(sb.toString());
        query = DaoSqlUtil.appendQuery(query, "pid", pid);
        query = DaoSqlUtil.appendQuery(query, "level", level);
        
		List<Object[]> queryResult = query.list();

		for (Object[] fields : queryResult) {
			IdNamePid idAndName = new IdNamePid();
			idAndName.setId(fields[0] == null ? 0 : Long.parseLong(fields[0].toString()));
			idAndName.setName(fields[1] == null ? "" : fields[1].toString());
			idAndName.setPid(pid);
			result.add(idAndName);
		}
		return result;
	}


	@SuppressWarnings("unchecked")
	public List<IdName> getIdNameByLevelAndPid (Integer level, Long pid, Integer status) {

		StringBuffer sb = new StringBuffer("select id, name from area where 1=1 ");
		sb = DaoSqlUtil.appendSql(sb, "pid", pid);
		sb = DaoSqlUtil.appendSql(sb, "level", level);
		sb = DaoSqlUtil.appendSql(sb, "status", status);
		sb.append("order by sort asc");

		Query query = hibernateDao.getSqlQuery(sb.toString());
		query = DaoSqlUtil.appendQuery(query, "pid", pid);
		query = DaoSqlUtil.appendQuery(query, "level", level);
		query = DaoSqlUtil.appendQuery(query, "status", status);

		List<Object[]> queryResult = query.list();

		List<IdName> result = Lists.newArrayList();
		for (Object[] fields : queryResult) {
			IdName idAndName = new IdName();
			idAndName.setId(fields[0] == null ? 0 : Long.parseLong(fields[0].toString()));
			idAndName.setName(fields[1] == null ? "" : fields[1].toString());
			result.add(idAndName);
		}
		return result;
	}

}
